package com.luom.fsp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luom.fsp.annotation.AuthCheck;
import com.luom.fsp.common.BaseResponse;
import com.luom.fsp.common.DeleteRequest;
import com.luom.fsp.common.ErrorCode;
import com.luom.fsp.common.ResultUtils;
import com.luom.fsp.constant.UserConstant;
import com.luom.fsp.exception.BusinessException;
import com.luom.fsp.exception.ThrowUtils;
import com.luom.fsp.model.dto.cart.CartAddRequest;
import com.luom.fsp.model.dto.cart.CartQueryRequest;
import com.luom.fsp.model.dto.cart.CartUpdateRequest;
import com.luom.fsp.model.entity.Cart;
import com.luom.fsp.model.entity.User;
import com.luom.fsp.model.vo.CartVO;
import com.luom.fsp.service.CartService;
import com.luom.fsp.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 购物车接口
 *
*/
@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {

    @Resource
    private CartService cartService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建购物车
     *
     * @param cartAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addCart(@RequestBody CartAddRequest cartAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(cartAddRequest == null, ErrorCode.PARAMS_ERROR);
        Cart cart = new Cart();
        BeanUtils.copyProperties(cartAddRequest, cart);
        // 数据校验
        cartService.validCart(cart, true);
        if (cart.getUserId() == null){
            User loginUser = userService.getLoginUser(request);
            cart.setUserId(loginUser.getId());
        }
        // 写入数据库
        boolean result = cartService.save(cart);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        // 返回新写入的数据 id
        long newCartId = cart.getId();
        return ResultUtils.success(newCartId);
    }

    /**
     * 删除购物车
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteCart(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        Cart oldCart = cartService.getById(id);
        ThrowUtils.throwIf(oldCart == null, ErrorCode.NOT_FOUND_ERROR);
        // 仅本人或管理员可删除
        if (!oldCart.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        // 操作数据库
        boolean result = cartService.removeById(id);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 更新购物车（仅管理员可用）
     *
     * @param cartUpdateRequest
     * @return
     */
    /*@PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateCart(@RequestBody CartUpdateRequest cartUpdateRequest) {
        if (cartUpdateRequest == null || cartUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // todo 在此处将实体类和 DTO 进行转换
        Cart cart = new Cart();
        BeanUtils.copyProperties(cartUpdateRequest, cart);
        // 数据校验
        cartService.validCart(cart, false);
        // 判断是否存在
        long id = cartUpdateRequest.getId();
        Cart oldCart = cartService.getById(id);
        ThrowUtils.throwIf(oldCart == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = cartService.updateById(cart);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }*/

    /**
     * 根据 id 获取购物车（封装类）
     *
     * @param id
     * @return
     */
    /*@GetMapping("/get/vo")
    public BaseResponse<CartVO> getCartVOById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Cart cart = cartService.getById(id);
        ThrowUtils.throwIf(cart == null, ErrorCode.NOT_FOUND_ERROR);
        // 获取封装类
        return ResultUtils.success(cartService.getCartVO(cart, request));
    }*/

    /**
     * 分页获取购物车列表（仅管理员可用）
     *
     * @param cartQueryRequest
     * @return
     */
    /*@PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Cart>> listCartByPage(@RequestBody CartQueryRequest cartQueryRequest) {
        long current = cartQueryRequest.getCurrent();
        long size = cartQueryRequest.getPageSize();
        // 查询数据库
        Page<Cart> cartPage = cartService.page(new Page<>(current, size),
                cartService.getQueryWrapper(cartQueryRequest));
        return ResultUtils.success(cartPage);
    }*/

    /**
     * 分页获取购物车列表（封装类）
     *
     * @param cartQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<CartVO>> listCartVOByPage(@RequestBody CartQueryRequest cartQueryRequest,
                                                               HttpServletRequest request) {
        long current = cartQueryRequest.getCurrent();
        long size = cartQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<Cart> cartPage = cartService.page(new Page<>(current, size),
                cartService.getQueryWrapper(cartQueryRequest));
        // 获取封装类
        return ResultUtils.success(cartService.getCartVOPage(cartPage, request));
    }

    /**
     * 分页获取当前登录用户创建的购物车列表
     *
     * @param cartQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<CartVO>> listMyCartVOByPage(@RequestBody CartQueryRequest cartQueryRequest,
                                                                 HttpServletRequest request) {
        ThrowUtils.throwIf(cartQueryRequest == null, ErrorCode.PARAMS_ERROR);
        // 补充查询条件，只查询当前登录用户的数据
        User loginUser = userService.getLoginUser(request);
        cartQueryRequest.setUserId(loginUser.getId());
        long current = cartQueryRequest.getCurrent();
        long size = cartQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<Cart> cartPage = cartService.page(new Page<>(current, size),
                cartService.getQueryWrapper(cartQueryRequest));
        // 获取封装类
        return ResultUtils.success(cartService.getCartVOPage(cartPage, request));
    }

    // endregion
}
